package com.book.warehouse.controller;

import com.book.common.annotation.ControllerEndpoint;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.FebsUtil;
import com.book.warehouse.entity.WarehouseHouse;
import com.book.warehouse.service.WarehouseHouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: 刘恬镯
 * @Description:  20174052
 */

@Controller
public class WarehouseHouseController extends BaseController{
    @Autowired
    WarehouseHouseService warehouseHouseService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "warehouse/warehouseHouse")
    public String warehouseHouseIndex() {
        return FebsUtil.view("warehouse/warehouseHouse/warehouseHouseList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "warehouse/warehouseHouse/add")
    public String warehouseHouseAdd() {
        return FebsUtil.view("warehouse/warehouseHouse/warehouseHouseAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "warehouse/warehouseHouse/update/{id}")
    public String warehouseHouseUpdate(@PathVariable Integer id, ModelMap modelMap) {
        WarehouseHouse warehouseHouse = this.warehouseHouseService.getById(id);
        modelMap.put("warehouseHouse", warehouseHouse);
        return FebsUtil.view("warehouse/warehouseHouse/warehouseHouseUpdate");
    }

    @GetMapping("warehouseHouse")
    @ResponseBody
    @RequiresPermissions("warehouseHouse:view")
    public FebsResponse getAllType(WarehouseHouse warehouseHouse) {
        return new FebsResponse().success().data(this.warehouseHouseService.findWarehouseHouse(warehouseHouse));
    }

    @Log("访问仓库信息")
    @GetMapping("warehouseHouse/list")
    @ResponseBody
    @RequiresPermissions("warehouseHouse:view")
    public FebsResponse warehouseHouseIPage(QueryRequest request, WarehouseHouse warehouseHouse) {
        Map<String, Object> dataTable = getDataTable(this.warehouseHouseService.pageWarehouseHouse(request, warehouseHouse));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("仓库-新增")
    @PostMapping("warehouseHouse")
    @ResponseBody
    @RequiresPermissions("warehouseHouse:add")
    @ControllerEndpoint(operation = "新增仓库信息成功", exceptionMessage = "新增仓库信息失败")
    public FebsResponse warehouseHouseAdd(WarehouseHouse warehouseHouse) {
        warehouseHouse.setHousebooks(0);
        this.warehouseHouseService.addWarehouseHouse(warehouseHouse);
        return new FebsResponse().success();
    }

    @Log("仓库-修改")
    @PostMapping("warehouseHouse/update")
    @ResponseBody
    @RequiresPermissions("warehouseHouse:update")
    @ControllerEndpoint(operation = "修改仓库信息成功", exceptionMessage = "修改仓库信息失败")
    public FebsResponse warehouseHouseUpdate(WarehouseHouse warehouseHouse) {
        this.warehouseHouseService.updateWarehouseHouse(warehouseHouse);
        return new FebsResponse().success();
    }

    @Log("仓库-删除")
    @PostMapping("warehouseHouse/delete/{ids}")
    @ResponseBody
    @RequiresPermissions("warehouseHouse:delete")
    @ControllerEndpoint(operation = "删除仓库信息成功", exceptionMessage = "删除仓库信息失败")
    public FebsResponse warehouseHousedelete(@PathVariable String ids) {
        String[] attr = ids.split(",");
        List<WarehouseHouse> warehouseHouses = (List<WarehouseHouse>) warehouseHouseService.listByIds(Arrays.asList(attr));
        for (int i = 0; i < warehouseHouses.size(); i++) {
            if (warehouseHouses.get(i).getHousebooks() > 0) {
                return new FebsResponse().message("该仓库还存有书籍，不能删除！");
            }
        }
        this.warehouseHouseService.deleteWarehouseHouse(attr);
        return new FebsResponse().success();
    }
}
